﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic;

namespace WebApp.Space.AccountInfo
{
    public partial class UserInfoEdit : PageBase
    {
        private UserManager m_UserManager = new UserManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser == null)
            {
                //Response.Redirect("~/Login.aspx");
                Page.RegisterClientScriptBlock("key", "<script>window.top.location = '/Login.aspx';</script>");
                return;
            }
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            this.txtNickName.Text = SignInUser.NickName;
            this.txtPhone.Text = SignInUser.tel;
            this.txtMail.Text = SignInUser.Email;
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            //WebGeneric.WebMessageBox("添加成功!", this.Page);

            string NickName = this.txtNickName.Text;
            string Phone = this.txtPhone.Text;// = SignInUser.tel;
            string Mail = this.txtMail.Text;// = SignInUser.Email;

            string pass = txtNewPass.Text;

            Cosmetic.DataEntity.User user = m_UserManager.GetUserById(SignInUser.UserId);


            if (this.txtNewPass.Text.Trim()!="")
            {
                if (this.txtOldPass.Text.Trim() != user.Password)
                {
                    WebGeneric.WebMessageBox("旧密码不正确!", this.Page);
                    return;
                }
                if (this.txtNewPass.Text.Trim() != this.txtConfirmPass.Text.Trim())
                {
                    WebGeneric.WebMessageBox("两次输入的密码不相同!", this.Page);
                    return;
                }
                user.Password = pass;
            }

            user.NickName = NickName;
            user.tel = Phone;
            user.Email = Mail;

            m_UserManager.Update();

            this.SignInUser = user;

            Page.RegisterClientScriptBlock("alert", "<script>alert('更新成功!');location.href='UserInfo.aspx'</script>");

            //WebGeneric.WebMessageBox("添加成功!", this.Page);
        }
    }
}